package com.choudou5.solr.analyzer.utils;

import com.choudou5.solr.util.StrUtil;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 拼音工具类
 */
public class PinyinUtil {

    public static void main(String[] args) throws Exception {
        String str = "重庆市";
        System.out.println(getPinYin(str));
        System.out.println(getShortPinYin(str));
        System.out.println(getHeadFirstLetter(str));
        List<String> array = getPinyinArray(str);
        for (String py : array) {
            System.out.println(py);
        }
    }


    /**
     * 中文 转 拼音
     * @param chars
     */
    public static List<String> getPinyinArray(char[] chars) {
        if(chars == null)
            return Collections.EMPTY_LIST;
        return getPinyinArray(String.valueOf(chars));
    }


    /**
     * 中文 转 拼音
     * @param text
     */
    public static List<String> getPinyinArray(String text) {
        if(StrUtil.isBlank(text))
            return Collections.EMPTY_LIST;
        char[] chars = text.toCharArray();
        List<String> pinyins = new ArrayList<>();
        pinyins.add(getPinYin(text));//全拼
        pinyins.add(getShortPinYin(text));//简拼
        for (int i = 0; i < chars.length; i++) {
            if (isChinese(chars[i])) {// 中文
                try {
                    String[] strs = PinyinHelper.convertToPinyinArray(chars[i], PinyinFormat.WITHOUT_TONE);
                    if (strs != null) {
                        pinyins.add(strs[0]);
                        // 多音字
                        if (strs.length > 1) {
                            pinyins.add(strs[1]);
                        }
                    }
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return pinyins;
    }



    /**
     * 获得 拼音 (全拼)
     * @param str
     * @return
     */
    public static String getPinYin(String str){
        String pinyin = null;
        try {
            pinyin = PinyinHelper.convertToPinyinString(str, "", PinyinFormat.WITHOUT_TONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pinyin;
    }


    /**
     * 获得 短拼
     * @param str
     * @return
     */
    public static String getShortPinYin(String str){
        String shortPinYin = null;
        try {
            shortPinYin = PinyinHelper.getShortPinyin(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return shortPinYin;
    }

    /**
     * 获得 首字母
     * @param str
     * @return
     */
    public static String getHeadFirstLetter(String str) {
        String shortPinYin = getShortPinYin(str);
        return ToolkitUtil.isNotBlank(shortPinYin)?String.valueOf(shortPinYin.charAt(0)):null;
    }

    public static boolean isChinese(char c) {
        return (c >= 19968) && (c <= 171941);
    }

    public static boolean containsChinese(String s) {
        if ((null == s) || ("".equals(s.trim()))) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (isChinese(s.charAt(i))) {
                return true;
            }
        }
        return false;
    }

}
